package com.xiaoyu.lanling.view

import `in`.srain.cube.util.LocalDisplay
import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.facebook.drawee.generic.GenericDraweeHierarchy
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.UserAvatarUpdateEvent
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.base.view.OnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.media.image.UserImageLoadParam
import com.xiaoyu.lanling.router.Router
import org.greenrobot.eventbus.Subscribe

class UserAvatarDraweeView : SimpleDraweeView {

    private var shouldShowBorder = false
    private var size = 0
    private var borderWidthDp = 0

    constructor(context: Context?, hierarchy: GenericDraweeHierarchy?) : super(context, hierarchy) {}
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {}

    override fun onAttach() {
        super.onAttach()
        AppEventBus.getInstance().register(this)
    }

    override fun onDetach() {
        super.onDetach()
        AppEventBus.getInstance().unregister(this)
    }

    @JvmOverloads
    fun allowJumpToUserPage(chatId: String? = null) {
        setOnClickListener(sOnClickListener)
        setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_CHAT_ID, chatId)
    }

    @Subscribe
    fun onEvent(event: UserAvatarUpdateEvent) {
        val user: User? = getViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER)
        user?.let {
            if (user == event.user) {
                val imageLoadParam: ImageLoadParam? = UserImageLoadParam.newBuilder().setUserAndDP(event.user, size).build()
                val imageLoadParamWithBoarder: ImageLoadParam? = UserImageLoadParam.newBuilder().setAutoPlay(false)?.setUserInfoAndDPWithBorder(event.user, 80, LocalDisplay.px2dp(1f), R.color.user_info_avatar_border, false)?.build()
                post {
                    ImageViewUtil.loadImage(this@UserAvatarDraweeView, if (shouldShowBorder) imageLoadParamWithBoarder else imageLoadParam)
                }
            }
        }
    }

    fun setBorderForShow(size: Int, borderWidthDp: Int) {
        shouldShowBorder = true
        this.size = size
        this.borderWidthDp = borderWidthDp
    }

    companion object {
        private val sOnClickListener: OnClickDebounceListener = object : OnClickDebounceListener() {
            override fun onClickDebounce(v: View) {
                val activity: Activity = App.getInstance().topActivity ?: return
                val user: User? = v.getViewTag<User>(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER)
                val chatId: String? = v.getViewTag<String>(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_CHAT_ID)
                if (user == null || user.isNobody) {
                    return
                }
                Router.instance.gotoUserActivity(activity, user)
            }
        }
    }
}